package com.lc.email.service.impl;

import com.lc.email.EmailUtil;
import com.lc.email.api.EmailService;
import com.lc.email.bo.SendEmailBo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.mail.MessagingException;

@Service
public class EmailServiceImpl implements EmailService {

    @Autowired
    private EmailUtil mailSender;

    @Override
    public boolean send(SendEmailBo bo) {
        //check
        if(bo == null || bo.getEmail() == null || bo.getCode() == null){
            return false;
        }
        //todo 发送邮件
        try {
            mailSender.send(bo.getEmail(), getEmailTemplate(bo.getCode()));
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public String getEmailTemplate(String code){
        StringBuilder sb = new StringBuilder();
        sb.append("欢迎注册www.test.com网站用户,您的验证码为: ").append(code).append(" ,验证码在十分钟内有效!");
        return sb.toString();
    }
}
